﻿using System.Collections.Generic;
using Northwind.Data;
using System.Linq;
using Northwind.Interfaces;

namespace Northwind.Business
{
    public class NorthwindServiceRepository : INorthwindRepository
    {
        private readonly NorthwindEntities _northwindEntities
            = new NorthwindEntities();

        public IEnumerable<Customer> GetAllCustomersNameAndID()
        {
            List<Customer> customerEntities = new List<Customer>();
            var customers = from c in _northwindEntities.Customers
                            select new {c.CustomerID, c.CompanyName};
            foreach (var customer in customers)
            {
                customerEntities.Add(new Customer()
                                         {
                                             CustomerID = customer.CustomerID,
                                             CompanyName = customer.CompanyName
                                         });
            }
            return customerEntities;
        }

        public Order GetOrder(string orderID)
        {
            int id = int.Parse(orderID);
            return (from o in _northwindEntities.Orders
                   where o.OrderID == id
                   select o).FirstOrDefault();
        }

        public IEnumerable<Order> GetOrders(string customerID)
        {
            return (from o in _northwindEntities.Orders
                   where o.CustomerID == customerID
                   select o).ToList<Order>();
        }

        public IEnumerable<Order_Detail> GetOrderDetails(int orderID)
        {
            return (from od in _northwindEntities.Order_Details
                   where od.OrderID == orderID
                   select od).ToList<Order_Detail>();
        }


        public Customer GetCustomerByID(string customerID)
        {
            return (from c in _northwindEntities.Customers
                    where c.CustomerID == customerID
                    select c).FirstOrDefault();
        }

        public IEnumerable<Employee> GetEmployees()
        {
            return _northwindEntities.Employees.ToList<Employee>();
        }

        public IEnumerable<Product> GetProducts()
        {
            return _northwindEntities.Products.ToList<Product>();
        }

        public void SaveChanges()
        {
            _northwindEntities.SaveChanges();
        }
    }
}
